package cn.sinjet.carassist;


import com.mgroup.wazehelper.R;

import android.app.Instrumentation;
import android.content.Context;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import cn.sinjet.communication.broadcast.SinjetBroadcastHandler;
import cn.sinjet.model.carassist.AppModel;
import cn.sinjet.viewmodel.ViewModel;

public class OsdMenu implements OnClickListener{
	public static String tag = "PopupMenu";
	Context context;
	private View View;
	private WindowManager wm;
	private WindowManager.LayoutParams params = new WindowManager.LayoutParams();;
	private PopupWindow popupWindow = null;
//	private MainActivity mParent = null;
	private int width = 295;
	private int height = 48;
    RadioButton mNaviMute;
	View mOsdMenuView ;
	boolean bShow = false;
	public OsdMenu(Context context){
		this.context = context;
//		mMainOsdView = new FrameLayout(context);
        wm = (WindowManager) context.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
		
		params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT
										| WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;
		params.flags = LayoutParams.FLAG_NOT_FOCUSABLE
										| LayoutParams.FLAG_NOT_TOUCH_MODAL;
		
		params.type = 2003;
		params.format = 1;
		
//		params.width = WindowManager.LayoutParams.WRAP_CONTENT;
//		params.height = WindowManager.LayoutParams.WRAP_CONTENT;
		params.x = 0;
		params.y = 0;
		
		params.alpha = 1;
		params.gravity =  Gravity.TOP | Gravity.CENTER_HORIZONTAL;
		 mOsdMenuView = LayoutInflater.from(context).inflate(R.layout.osd_menu, null,false);
//		mMainOsdView.addView(popupWindow_view);
		wm.addView(mOsdMenuView, params);
		bShow = true;
		mOsdMenuView.findViewById(R.id.osd_prev).setOnClickListener(this);
		mOsdMenuView.findViewById(R.id.osd_play).setOnClickListener(this);
		mOsdMenuView.findViewById(R.id.osd_pause).setOnClickListener(this);
		mOsdMenuView.findViewById(R.id.osd_next).setOnClickListener(this);
		mOsdMenuView.findViewById(R.id.osd_back).setOnClickListener(this);
		mOsdMenuView.findViewById(R.id.osd_navi_mute).setOnClickListener(this);
		
		mNaviMute = (RadioButton)mOsdMenuView.findViewById(R.id.osd_navi_mute);
		mNaviMute.setOnClickListener(this);
		mNaviMute.setChecked(AppModel.getInstance().getSettings().isNaviVoiceOn);
	   /* mOsdMenuView.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View view, MotionEvent me) {
				// TODO Auto-generated method stub
					wm.removeView(mOsdMenuView);
				    return false;
				}
			});*/
	}
	//public interface on
	private void popupMenuWindow(View view) {
		// TODO Auto-generated method stub
		getPopupWindow();
		popupWindow.showAsDropDown(view);
	}
	private void getPopupWindow(){
		if (null != popupWindow) {
			popupWindow.dismiss();
			return;
		} else {
			initPopupWindow();
		}
	}
	protected void initPopupWindow() {
		// TODO Auto-generated method stub
		View popupWindow_view = LayoutInflater.from(context).inflate(R.layout.osd_menu, null,false);
		// 创建PopupWindow实例,200,150分别是宽度和高度
		popupWindow = new PopupWindow(popupWindow_view,width, height, false);
		// 设置动画效果
		//popupWindow.setAnimationStyle(R.style.AnimationFade);
		popupWindow.setOutsideTouchable(true);
		//点击其他地方消失
		popupWindow_view.setOnTouchListener(new OnTouchListener() {
		
		@Override
		public boolean onTouch(View view, MotionEvent me) {
			// TODO Auto-generated method stub
				if (popupWindow != null && popupWindow.isShowing()) {
					popupWindow.dismiss();
					popupWindow = null;
				}
			    return false;
			}
		});
        
	}

	public boolean isShow(){
		if(popupWindow != null)
			return popupWindow.isShowing();
		else 
			return false;
	}
	
	public void showOrHide(View view,boolean bShow){
		if(bShow){
			if (popupWindow != null && popupWindow.isShowing())
				return;
			popupMenuWindow(view);
			popupWindow.update(300,300, -1, -1);
		}else {
			if (popupWindow != null && popupWindow.isShowing()) {
				popupWindow.dismiss();
				popupWindow = null;
			}	
		}		
	}
	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
//		wm.removeView(mOsdMenuView);
		Log.d("ui", "onClick"+view.getId());
		switch(view.getId()){
		case R.id.osd_back:
			wm.removeView(mOsdMenuView);
			bShow = false;
			break;
		case R.id.osd_next:
			if(SinjetApplication.getInstance().isMediaFocuseOn())
				SinjetBroadcastHandler.requestPlayNext(context);
			else 
				emulateMediaButton(KeyEvent.KEYCODE_MEDIA_NEXT);
			break;
		case R.id.osd_play:
			if(SinjetApplication.getInstance().isMediaFocuseOn())
			      SinjetBroadcastHandler.requestPlay(context);
		    else 
			      emulateMediaButton(KeyEvent.KEYCODE_MEDIA_PLAY);
			break;
		case R.id.osd_pause:
			if(SinjetApplication.getInstance().isMediaFocuseOn())
			      SinjetBroadcastHandler.requestPause(context);
		    else 
			      emulateMediaButton(KeyEvent.KEYCODE_MEDIA_PAUSE);
			break;
		case R.id.osd_prev:
			if(SinjetApplication.getInstance().isMediaFocuseOn())
			      SinjetBroadcastHandler.requestPlayPrev(context);
		    else 
		    	emulateMediaButton(KeyEvent.KEYCODE_MEDIA_PREVIOUS);
			break;
		case R.id.osd_navi_mute:
			AppModel.getInstance().getSettings().isNaviVoiceOn = !AppModel.getInstance().getSettings().isNaviVoiceOn;
			mNaviMute.setChecked(AppModel.getInstance().getSettings().isNaviVoiceOn);
			int checked = (AppModel.getInstance().getSettings().isNaviVoiceOn)?1:0;
			ViewModel.getIns().setViewProperty(R.id.navi_set_navi_voice,checked);
			break;
		}
	}
	
	 private void emulateMediaButton(final int keyCode){
		 new Thread(){
				public void run(){
					try{
						Instrumentation inst = new Instrumentation();
						inst.sendKeyDownUpSync(keyCode);
					} catch (Exception e){
					}
				}
			 }.start();
	 }
	 /*
	private void sendDownEvents(int keyCode) {  
		  final long token = Binder.clearCallingIdentity();  
		   // Inject down.  
		   final long downTime = SystemClock.uptimeMillis();  
		   KeyEvent down = KeyEvent.obtain(downTime, downTime, KeyEvent.ACTION_DOWN, keyCode, 0, 0,  
		     KeyCharacterMap.VIRTUAL_KEYBOARD, 0, KeyEvent.FLAG_FROM_SYSTEM,  
		     InputDevice.SOURCE_KEYBOARD, null);  
		   InputManager.getInstance().injectInputEvent(down,  
		     InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);  
		   down.recycle();  
		   
		   Binder.restoreCallingIdentity(token);  
		  }  
	}*/
	public void dismiss() {
		// TODO Auto-generated method stub
		if(mOsdMenuView != null && bShow){
			wm.removeView(mOsdMenuView);
			bShow = false;
		}
	}
}
